jdtsmith / indent-bars

Fast, configurable indentation guide-bars for Emacs
GNU General Public License v3.0
272 stars 7 forks source link
emacs programming

indent-bars: fast, configurable indentation guide-bars for Emacs

FAQINSTALLCUSTOMIZEMORE DETAILS

This package provides indentation guide bars in Emacs, with optional tree-sitter enhancement:

What's New

FAQ's

Install/config

Not yet in a package database; simply clone and point use-package at the correct path. You can also simply use the vc-package-install command newly released with Emacs 29.

(use-package indent-bars
  :load-path "~/code/emacs/indent-bars"
  :hook ((python-mode yaml-mode) . indent-bars-mode)) ; or whichever modes you prefer

Straight

To clone with use-package and straight:

(use-package indent-bars
  :straight (indent-bars :type git :host github :repo "jdtsmith/indent-bars")
  :hook ((python-mode yaml-mode) . indent-bars-mode)) ; or whichever modes you prefer

With tree-sitter support

(use-package indent-bars
  :load-path "~/code/emacs/indent-bars"
  :config
  (require 'indent-bars-ts)         ; not needed with straight
  :custom
  (indent-bars-treesit-support t)
  (indent-bars-treesit-ignore-blank-lines-types '("module"))
  ;; Add other languages as needed
  (indent-bars-treesit-scope '((python function_definition class_definition for_statement
      if_statement with_statement while_statement)))
  ;; wrap may not be needed if no-descend-list is enough
  ;;(indent-bars-treesit-wrap '((python argument_list parameters ; for python, as an example
  ;;                      list list_comprehension
  ;;                      dictionary dictionary_comprehension
  ;;                      parenthesized_expression subscript)))
  :hook ((python-base-mode yaml-mode) . indent-bars-mode))

See tree-sitter, and also the Wiki page.

Compatibility

[!IMPORTANT] For indent-bars to display fancy guide bars, your port and version of emacs must correctly display the :stipple face attribute. Most do, but some do not.

Known :stipple support, by Emacs build:

[^1]: Most easily installed with brew.

Please open an issue with any updates/corrections to this list. See also Testing Stipples.

indent-bars can also be used without stipples, drawing a simple vertical character (like ) instead. It automatically does this in non-graphical displays (terminals), but this can be made the default; see Character Display.

Customization

M-x customize-group indent-bars is the easiest way to customize everything about the appearance and function of indent-bars (check sub-groups too). There are many customization variables and bar styling in particular is highly configurable, so use Customize!

[!TIP] The easiest way to achieve a particular style is to customize the groups indent-bars, sub-group indent-bars-style and (if you use TS) indent-bars-ts + indent-bars-ts-style. While in the Customize interface, pull up one of your buffers with bars in another window on the same frame. When you make changes to variables (C-c C-c is convenient in custom buffers), the bar style/etc. will automatically update. When you are happy, you can either "Set for Future Sessions", or "Show Saved Lisp Expression" for the variables you changed and copy them into your init file.

See some examples with relevant settings.

The main customization variables are categorized below. See the documentation of each variable for more details.

Bar colors

Custom variables for configuring bar color, including depth-based palettes:

Bar shape and size

Variables affecting the visual appearance of bars (color aside):

Current Depth highlighting

Configuration for highlighting the current indentation bar depth:

Bar setup and location

Configuration variables for bar position and line locations (including on blank lines):

Character-based bars and terminal

Custom variables affecting character-based bar display, e.g. in the terminal:

Tree-sitter

For more information, check the details.

Main treesitter configuration variables

Tree-sitter alternate styling variables

By default, if tree-sitter and scope focus are active (indent-bars-treesit-scope), the style and highlight settings above apply only to the in-scope bars[^2]. You can separately configure an alternate style for the appearance of the out-of-scope bars — i.e. the bars outside the current tree-sitter scope[^2]. Usually you'd want to de-emphasize out-of-scope bars somehow, but that's not required (go crazy).

[^2]: Or visa versa if indent-bars-ts-styling-scope is set to in-scope.

To customize the alternate bar appearance, you use the parallel set of custom variables with an indent-bars-ts- prefix. Each of these variables can be set similarly to their default counterparts to fully configure alternate bar appearance, including color, depth highlighting, bar pattern, etc.

You can interchange the role of in-scope and out-of-scope using indent-bars-ts-styling-scope. This is useful if you prefer to have the default style (e.g. the bar style in non-tree-sitter-enabled buffers) match the out-of-scope style within tree-sitter buffers (i.e. if you want to emphasize scope, not de-emphasize out-of-scope).

[!NOTE] Scope focus highlighting is completely independent of current depth highlighting, and you can style them separately, or enable one or the other, both, or neither.

The ts custom variables for configuring the alternate styling are:

Each of these parallel variables has the same form as their equivalent non-ts version (the "parent" variable), with two difference:

  1. Some (marked with [I] above) can optionally use inheritance from their parent. Inheritance means any missing :key based elements are inherited from the in-scope (parent) style. To configure their inheritance, you can optionally set these variable values to a cons cell of the form ([no-]inherit . value), where value has the normal format for the parent variable. inherit (the default, if the cons cell is omitted and value is simply used as-is) means that any unspecified :key values are inherited from the parent variable. The symbol no-inherit means to omit any missing key values for the alternate styling.
  2. For any non-:key type values, the specific symbol value 'unspecified can be set to indicate using the parent's value for that slot.

For example, a setting of:

(setopt indent-bars-ts-color '(inherit unspecified :blend 0.15))

means to configure the color of alternate style bars as follows:

  1. use the color from the parent variable indent-bars-color (since it is unspecified here)
  2. set :blend to 0.15
  3. inherit any other missing keyword values from indent-bars-color

The easiest way to configure inheritance and unspecified values in the ts variables is via the customize interface; see the customize group indent-bars-ts-style.

Details and Caveats

Indentation

indent-bars works with either space- or tab-based indentation. If possible, prefer space indentation, as it is faster. Note that some modes explicitly enable or disable indent-tabs-mode.

Current Depth Highlight

indent-bars can highlight the bar at the current depth, and supports a few different ways to determine which bar gets selected for highlight (see indent-bars-highlight-selection-method):

  1. nil: The simplest version selects the depth of the last-visible bar on the current line for highlight.
  2. on-bar: The old default, which selects the depth of the "unseen" bar that the first character of text on the current line covers up.
  3. context: The new default, a blend of these two. It selects the last-visible bar unless an adjacent non-blank line is indented deeper by at least one indent spacing, in which case the on-bar approach is used.

Experiment with these to see what you prefer.

Tree-sitter Details

indent-bars can optionally use tree-sitter in supported files to enable several features:

  1. Scope Focus: The current tree-sitter scope can be focused, with out-of-scope bars de-emphasized or in-scope bars emphasized (or actually, styled however you want). This can be configured by specifying matching "scope" node types (e.g. functions, blocks, etc.) for each language of interest. The innermost node (covering sufficient lines) will then be rendered distinctly from out-of-scope bars.
  2. Selective Blank Line Display: By default, indent-bars displays bars on blank lines (though this can be configured), so that they remain continuous. It can be nice to omit the display of blank lines bars at the top structural level (e.g. in a module), to make divisions between top-level constructs more visible. Tree-sitter can help indent-bars identify those lines.
  3. Wrap Detection: It can be useful to prevent excess bars inside wrapped entities which move indent to "line things up." These include things like argument lists, literal dictionaries, or other heirarchical multi-line structures. Tree-sitter can help detect these and inhibit unwanted bars (but see also indent-bars-no-descend-string/list, which do not require tree-sitter).

[!NOTE] indent-bars' tree-sitter capabilities require Emacs 29 or later built with tree-sitter support, and the appropriate tree-sitter grammars installed for your languages of interest. Additional node type configuration by language is required; see below.

Configuring tree-sitter

Scope

Simply configure indent-bars-treesit-scope with the languages and node types for which "local scope" highlighting nodes are of interest. This must be done for each tree-sitter language you use. This scope could be as granular as classes and functions, or include detailed block statements. You can disable scoping for "short blocks" using indent-bars-treesit-scope-min-lines, so that, e.g., a quick if statement does not capture scope. I recommend starting with the minimal possible set of scope node types, adding as needed.

[TIP] If you don't know the name treesitter uses for your language, evaluate (treesit-language-at (point-min)) in a ts-enabled buffer.

Wrap

indent-bars-treesit-wrap can be configured in a similar manner (mapping language to wrapping node types). Note that indent-bars-no-descend-list, which does not require tree-sitter and is on by default, may be sufficient for your uses.

Ignore certain blank lines

You can assign a single (usually top-level) node type to ignore when drawing bars on blanks linkes; see indent-bars-treesit-ignore-blank-lines-types (which, please note, is configured as a list of strings, unlike indent-bars-treesit-wrap/scope).

Identifying treesit node types of interest

The easiest way to discover node types of interest (in a buffer with working treesit support) is to M-x treesit-explore-mode. Then simply highlight the beginning of a line of interest, and look in the treesitter explorer buffer which pops up for the names of obvious nodes in the tree. Add these types to indent-bars-treesit-scope/wrap for the language of interest, then M-x indent-bars-reset and see how you did (this will happen automatically if you make the change in the Customize interface).

Please document good tree-sitter settings for other languages in the Wiki.

Moving by columns

If indent-bars-display-on-blank-lines is set, the newline at the end of blank lines may have a 'display property set to show the bars. Emacs does not deal correctly with display properties containing newlines when moving by columns. This is not normally a problem, but in one instance it is a nuisance: evil-mode tries to "preserve" column during line moves, so can trigger this emacs misfeature. The symptom is that point jumps a line and moves over as you move down with evil. A workaround is here.

Display

Stipples

Stipples are repeating bitmap patterns anchored to the full emacs frame. indent-bars basically "opens windows" on this fixed pattern to "reveal" the bars.

The fast stipple method used for drawing bars enables lots of interesting patterns.

Testing Stipples

If you are experiencing issues with stipple bar display (missing, garbled, etc.), and would like to determine if stipples are working correctly in your build of emacs, you can test it as follows.

  1. In the *scratch* buffer, use first M-x font-lock-mode to disable fontification
  2. Hit C-x C-e just after the last ) in the following code:
    (let* ((w (window-font-width))
          (stipple `(,w 1 ,(apply #'unibyte-string
                   (append (make-list (1- (/ (+ w 7) 8)) ?\0)
                       '(1))))))
     (insert "\n" (propertize (concat  (make-string 15 ?\s)
                    "THIS IS A TEST"
                    (make-string 15 ?\s))
                              'face `(:background "red" :foreground "blue" :stipple ,stipple))))

This should then look something like (note the blue vertical bars):

image

If you determine that stipples do not work in your version of Emacs, consider upgrading to a version which supports them, reporting the bug, or setting indent-bars-prefer-character=t.

Per-buffer stipple offsets

To get the stipple bars to appear in the correct location within their column, indent-bars must consider the starting horizontal pixel position of the current window, and "rotate" the stipple pattern accordingly. It does this automatically, per buffer, so you shouldn't ever notice problems, even when re-sizing or re-arranging windows, changing font size, etc. Until v0.5, showing the same buffer side by side in Emacs versions which support pixel-level window width/offsets could lead to unexpected bar artifacts, since the offset applies per-buffer, not per-window. In v0.5, an alternate method for applying per-window stipple patterns was used to solve this.

Character display

For terminals, (and everywhere, if indent-bars-prefer-character is set), indent-bars will not attempt stipple display, but instead use simple characters (e.g. ; see an example).

Note that in mixed gui/terminal sessions of the same Emacs process, you may need to M-x indent-bars-reset when switching a given buffer between graphical and terminal frames.

Advantages/Disadvantages

Advantages of stipples

Advantages of character bar display

Speed

indent-bars was in part motivated by the inefficiency of older indentation highlight modes, and is designed for speed. It uses stipples (fixed bitmap patterns) and font-lock for fast and efficient bar drawing — simply faces on spaces. Highlighting the current indentation level is essentially free, since it works by filtered remapping of the relevant face.

The heaviest operations are tree-sitter support (especially scope highlighting), and blank-line highlighting. If you experience any speed issues, these are the first settings to experiment with. Using with tab-based indentation may also be slightly (but likely imperceptibly) slower than with space-based.

Both indentation-depth highlighting and current-tree-sitter-scope highlighting are protected by timers to avoid unnecessary loads (e.g. when pixel-scrolling). Note that indentation-depth highlighting is very fast and can safely be set to 0 seconds (though bars will then flash rapidly as you scroll). Tree-sitter scope requires querying the tree-sitter core, which can be somewhat slower, so be careful setting its timer too low.

Related Packages

Why a new package?

None of the existing packages:

  1. were fast enough with large files (including current depth highlighting)
  2. had enough guide appearance configurability
  3. were able to support depth-based coloring
  4. offered robust support for guides on blank lines
  5. had tree-sitter capabilities

Acknowledgments

I'm grateful for in-depth advice and input on the design of indent-bars from Eli Zaretski, Stefan Monnier, Dmitry Gutov and many other who opened issues and PRs.

highlight-indentation-mode was a source of good ideas, and indent-bars adapts the indentation guessing function from this mode. The original idea of using stipples for "better" indent-bars came from this comment by @vlcek.