Page MenuHomeFreeBSD

D57008.1793822056.diff
No OneTemporary

Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None

D57008.1793822056.diff

diff --git a/tools/tools/editing/freebsd.el b/tools/tools/editing/freebsd.el
--- a/tools/tools/editing/freebsd.el
+++ b/tools/tools/editing/freebsd.el
@@ -1,40 +1,107 @@
-;;; This function switches C-mode so that it indents almost everything
-;;; as specified in FreeBSD's style(9). Tested with emacs-19.34 and
-;;; xemacs-20.4.
-;;;
-;;; Use "M-x bsd" in a C mode buffer to activate it.
-;;;
-;;; The only problem I found is top-level indenting:
-;;;
-;;; We want function definitions with the function name at the beginning
-;;; of a second line after the return type specification in the first:
-;;; > int
-;;; > foo(int bla)
-;;; But emacs c-mode can't treat this differently from other multiple-line
-;;; toplevel constructs:
-;;; > const char *const bar =
-;;; > "sometext";
-;;; which means the second line must be indented by hand.
-;;;
-;;; To make this the default, use a line like this, but you can't easily
-;;; switch back to default GNU style, since the old state isn't saved.
-;;; (add-hook 'c-mode-common-hook 'bsd)
-;;; As long as you don't have this in the c-mode hook you can edit GNU
-;;; and BSD style C sources within one emacs session with no problem.
-;;;
-;;; Please report problems and additions directly to cracauer@freebsd.org
+;;; freebsd.el --- FreeBSD style(9) formatting -*- lexical-binding: t; -*-
-(defun bsd () (interactive)
- (c-set-style "bsd")
- (setq indent-tabs-mode t)
- ;; Use C-c C-s at points of source code so see which
- ;; c-set-offset is in effect for this situation
- (c-set-offset 'defun-block-intro 8)
- (c-set-offset 'statement-block-intro 8)
- (c-set-offset 'statement-case-intro 8)
- (c-set-offset 'substatement-open 4)
- (c-set-offset 'substatement 8)
- (c-set-offset 'arglist-cont-nonempty 4)
- (c-set-offset 'inclass 8)
- (c-set-offset 'knr-argdecl-intro 8)
- )
+;;; Commentary:
+
+;; This library customizes Emacs modes for editing C and related source code to
+;; support FreeBSD's style(9) formatting. It handles both the classic modes and
+;; the newer tree-sitter modes.
+;;
+;; After loading this file, use "M-x freebsd" to activate it.
+;;
+;; To make this the default, add the configuration below to your Emacs
+;; initialization.
+;;
+;; Classic modes:
+;; (add-hook 'c-mode-common-hook 'freebsd)
+;;
+;; Note: With the hook, you cannot easily switch back to default GNU style,
+;; since the old state isn't saved.
+;;
+;; Tree-sitter modes:
+;; (add-to-list 'major-mode-remap-alist '(c-mode . c-ts-mode))
+;; (add-to-list 'major-mode-remap-alist '(c++-mode . c++-ts-mode))
+;; (add-to-list 'major-mode-remap-alist '(c-or-c++-mode . c-or-c++-ts-mode))
+;;
+;; (add-hook 'c-ts-mode-hook 'freebsd)
+;; (add-hook 'c++-ts-mode-hook 'freebsd)
+;;
+;; For the classic modes, there are limitations with top-level indenting: We
+;; want function definitions with the function name at the beginning of a second
+;; line after the return type specification in the first:
+;;
+;; > int
+;; > foo(int bla)
+;;
+;; But emacs c-mode can't treat this differently from other multiple-line
+;; toplevel constructs:
+;;
+;; > const char *const bar =
+;; > "sometext";
+;;
+;; which means the second line must be indented by hand.
+;;
+;; Please report problems and additions to cracauer@freebsd.org and
+;; jrm@freebsd.org.
+
+;; Code:
+
+(defun freebsd--ts-indent-rules (&optional language)
+ "Return FreeBSD style(9) tree-sitter indent rules for LANGUAGE.
+LANGUAGE is \\='c or \\='cpp; defaults to \\='c if nil.
+Extends the built-in BSD style with style(9)-compliant 4-space
+continuation indentation for argument lists, parameter lists,
+multi-line expressions, and variable initializations."
+ (let ((continuation 4)
+ (offset 8)
+ (lang (or language 'c)))
+ (append
+ `(;; C++ access specifiers (public:, private:, protected:) at column 0
+ ((node-is "access_specifier") parent-bol 0)
+ ;; Closing braces at same level as opening construct
+ ((node-is "}") standalone-parent 0)
+ ;; C++ class members (after access specifier) at standard indent
+ ((parent-is "field_declaration_list") parent-bol ,offset)
+ ;; C++ field initializer list uses continuation indent
+ ((node-is "field_initializer_list") parent-bol ,continuation)
+ ;; Continuation: 4-space indent for wrapped arguments/parameters
+ ((parent-is "argument_list") standalone-parent ,continuation)
+ ((parent-is "parameter_list") standalone-parent ,continuation)
+ ;; Initializer list opening brace aligns with its declaration, not indented
+ ((match "initializer_list" "init_declarator") parent-bol 0)
+ ((parent-is "init_declarator") standalone-parent ,continuation)
+ ((parent-is "binary_expression") standalone-parent ,continuation)
+ ((parent-is "conditional_expression") standalone-parent ,continuation)
+ ((parent-is "assignment_expression") standalone-parent ,continuation)
+ ((parent-is "concatenated_string") first-sibling 0))
+ ;; Include the built-in BSD rules for the appropriate language
+ (alist-get lang (c-ts-mode--simple-indent-rules lang 'bsd)))))
+
+(defun freebsd ()
+ "Use FreeBSD style(9) formatting in the current buffer."
+ (interactive)
+ (setq-local indent-tabs-mode t)
+ (setq-local tab-width 8)
+ (if (derived-mode-p 'c-ts-mode 'c++-ts-mode)
+ ;; Tree-sitter
+ (progn
+ (c-ts-mode-set-style #'freebsd--ts-indent-rules)
+ (setq-local c-ts-indent-offset 8)
+ (setq-local treesit-font-lock-level 4))
+ ;; Classic c-mode
+ ;; Use C-c C-s at points of source code to see which
+ ;; c-set-offset is in effect for this situation.
+ (c-set-style "bsd")
+ (mapc (lambda (setting)
+ (c-set-offset (car setting) (cdr setting)))
+ '((defun-block-intro . 8)
+ (statement-block-intro . 8)
+ (statement-case-intro . 8)
+ (substatement-open . 4)
+ (substatement . 8)
+ (arglist-cont-nonempty . 4)
+ (inclass . 8)
+ (knr-argdecl-intro . 8)))))
+
+(provide 'freebsd)
+
+;;; freebsd.el ends here

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 4, 7:54 PM (11 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41448206
Default Alt Text
D57008.1793822056.diff (5 KB)

Event Timeline