Skip to content

Commit

Permalink
Add basic Emacs support
Browse files Browse the repository at this point in the history
  • Loading branch information
elizagamedev committed Nov 1, 2023
1 parent 919cfae commit 239e98c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .dir-locals.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
;;; Directory Local Variables -*- no-byte-compile: t -*-
;;; For more information see (info "(emacs) Directory Variables")

((c++-mode . ((c-file-style . "filament")
(apheleia-inhibit . t)))
(c-mode . ((c-file-style . "filament")
(apheleia-inhibit . t))))
79 changes: 79 additions & 0 deletions ide/emacs/c-filament-style.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
;;; c-filament-style.el --- Filament C++ style -*- lexical-binding: t -*-

;; Copyright (C) 2023 Google LLC

;; Author: Eliza Velasquez
;; Version: 0.1.0
;; Created: 2023-10-31
;; Package-Requires: ((emacs "24.1"))
;; Keywords: c
;; URL: https://github.com/google/filament
;; SPDX-License-Identifier: Apache-2

;;; Commentary:

;; Defines a basic Filament style for C++ code in Emacs.

;;; Code:

(require 'cc-mode)
(defvar c-syntactic-context)

(defun c-filament-style-lineup-brace-list-intro (langelem)
"Indent first line of braced lists in the Filament style.
This properly indents doubled-up arglists + lists, e.g. ({. It
also properly indents enums at 1x and other lists at 2x.
LANGELEM is the cons of the syntactic symbol and the anchor
position (or nil if there is none)."
(save-excursion
(let (case-fold-search)
(goto-char (c-langelem-pos langelem))
(if (looking-at "enum\\b")
c-basic-offset
(if (assq 'arglist-cont-nonempty c-syntactic-context)
(- c-basic-offset)
(* 2 c-basic-offset))))))

(defun c-filament-style-lineup-brace-list-entry (_langelem)
"Indent following lines in braced lists in the Filament style.
This properly indents doubled-up arglists + lists, e.g. ({."
(if (assq 'arglist-cont-nonempty c-syntactic-context)
(- (* c-basic-offset 2))
0))

(defun c-filament-style-lineup-arglist (langelem)
"Indent following lines in braced lists in the Filament style.
This properly indents arglists nested in if statements. LANGELEM
is the cons of the syntactic symbol and the anchor position (or
nil if there is none)."
(save-excursion
(let (case-fold-search)
(goto-char (c-langelem-pos langelem))
(if (and (cdr c-syntactic-context)
(looking-at "if\\b"))
c-basic-offset
(* 2 c-basic-offset)))))

(c-add-style "filament"
'((c-basic-offset . 4)
(c-offsets-alist
(innamespace . 0)
(inextern-lang . 0)
(arglist-intro . c-filament-style-lineup-arglist)
(arglist-cont . 0)
(arglist-cont-nonempty . c-filament-style-lineup-arglist)
(arglist-close . c-filament-style-lineup-arglist)
(statement-cont . ++)
(case-label . +)
(brace-list-intro . c-filament-style-lineup-brace-list-intro)
(brace-list-entry . c-filament-style-lineup-brace-list-entry)
(brace-list-close . c-filament-style-lineup-brace-list-entry)
(label . [0]))))

(provide 'c-filament-style)

;;; c-filament-style.el ends here

0 comments on commit 239e98c

Please sign in to comment.