Page MenuHomeFreeBSD

tools/tools/editing/freebsd.el: Add support for tree-sitter modes
Needs ReviewPublic

Authored by jrm on May 15 2026, 2:41 AM.
Tags
None
Referenced Files
F161797288: D57008.id177868.diff
Mon, Jul 6, 10:59 PM
Unknown Object (File)
Sun, Jul 5, 8:29 PM
Unknown Object (File)
Sat, Jun 27, 8:23 PM
Unknown Object (File)
Tue, Jun 23, 5:09 AM
Unknown Object (File)
Sun, Jun 21, 9:40 PM
Unknown Object (File)
Sun, Jun 21, 4:12 PM
Unknown Object (File)
Wed, Jun 17, 9:26 PM
Unknown Object (File)
Tue, Jun 9, 5:32 AM

Details

Reviewers
cracauer
Summary
  • Add custom indent rules to implement the style(9) 4-space continuation indent for argument lists, parameter lists, multi-line expressions, and variable initializations.
  • Rename the interactive function from bsd to freebsd to avoid a name collision with c-ts-mode's built-in 'bsd style symbol.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 73677
Build 70560: arc lint + arc unit

Event Timeline

jrm requested review of this revision.May 15 2026, 2:41 AM

Another minor comment tweak.

This revision is now accepted and ready to land.May 15 2026, 11:39 AM
(defconst knf-c-style
  '((c-basic-offset . 8)
    (c-comment-only-line-offset . 0)
    (c-offsets-alist . ((statement-block-intro . +)
                        (knr-argdecl-intro . +)
                        (substatement-open . 0)
                        (label . 0)
                        (statement-cont . +)
                        (inline-open . 0)
                        (inexpr-class . 0)
                        (statement-cont . *)
                        (arglist-cont . *)
                        (arglist-cont-nonempty *))))
  "BSD KNF")
(c-add-style "knf" knf-c-style)

is what I use.

Thanks for sharing. I'm going to spend more time testing these three configurations (@imp's, the classic one in freebsd.el, and the tree-sitter configuration) by opening lots of source files and doing things like C-M-\.

Here’s mine if it helps with any ideas: https://git.sr.ht/~patmaddox/lab/tree/trunk/item/dotfiles/emacs/lisp/freebsd-style-ts.el

;; C/C++ (FreeBSD style(9))
(require 'freebsd-style-ts)
(add-hook 'c-ts-mode-hook 'freebsd-c-style-ts)
(add-hook 'c++-ts-mode-hook 'freebsd-c-style-ts)

I’ve also got a test suite for formatting languages I use: https://git.sr.ht/~patmaddox/lab/tree/trunk/item/dotfiles/emacs/tests.sh

Lastly, I had proposed a patch for simplifying tree-sitter grammar builds (and also getting a lot more grammars): https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=292973 I don’t know if there’s interest in switching grammar sources, or perhaps adding it as a second option, but I’ve been using it and it’s working well so far.

  • Replace the hacky tree-sitter indent helper functions with cleaner rules from Pat's configuration with some minor tweaks:
    • Use c-ts-mode--simple-indent-rules instead of c-ts-mode--indent-styles and c-ts-indent-offset instead of c-ts-mode-indent-offset.
    • Add a fix for initializer list opening braces.
  • Provide, a closing footer, and an improved docstring.

When testing, a key limitation I'm seeing is unreliable indentation in files with heavy preprocessor use such as usr.bin/grep/grep.c.

Sample test:

emacs -Q
(add-to-list 'major-mode-remap-alist '(c-mode . c-ts-mode))
(load "/usr/src/tools/tools/editing/freebsd.el")
(find-file "/usr/src/usr.bin/grep/grep.c")
(freebsd)
(mark-whole-buffer)
(indent-region (point-min) (point-max))

This revision now requires review to proceed.Jun 4 2026, 7:09 PM

Thanks @guest-patmaddox. Much better. I haven't had much time to look into this or bug 292973. Do you have any thoughts on the shortcomings I mentioned in my last comment?